<%@ page import="java.util.Date"%>
<%@ page import="java.sql.Connection"%>
<%@ page import="java.sql.DriverManager"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.SQLException"%>
<%@ page import="java.sql.Statement"%>
<%@ page language="java" contentType="text/html; charset=windows-31j"
	pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>Jsp page</title>
</head>
<body>
	<h2>jsp</h2>
	<%
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		String schema = "testdb";
		String table = "testtable";
		String[] fields = { "id", "title" };
		int length = fields.length;

		long start;
		long end;
		long time;

		out.println("======== MYSQL =========");
		start = System.currentTimeMillis();
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String connectionUrl = "jdbc:mysql://localhost:3306/"
					+ schema;
			String connectionUser = "test";
			String connectionPassword = "test";
			conn = DriverManager.getConnection(connectionUrl,
					connectionUser, connectionPassword);
			stmt = conn.createStatement();
			rs = stmt.executeQuery("SELECT * FROM " + table);
			while (rs.next()) {
				for (int i = 0; i < length; i++) {
					String field = fields[i];
					String val = rs.getString(field);
					out.println("<p>" + field + ": " + val + "<p>");
				}
				out.println();
			}
		} catch (Exception e) {
			out.println(e);
			e.printStackTrace();
			return;
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		end = System.currentTimeMillis();
		time = end - start;

		out.println("====== PostgreSQL ======");
		start = System.currentTimeMillis();
		try {
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection(
					"jdbc:postgresql://153.122.19.224:5432/testdb", "test",
					"tets");
			stmt = conn.createStatement();
			String sqlStr = "SELECT * FROM testtable";
			rs = stmt.executeQuery(sqlStr);
			while (rs.next()) {
				int id = rs.getInt("id");
				String title = rs.getString("title");

				out.println(id + " " + title);
			}

		} catch (Exception e) {
			out.println(e.toString());
			return;
			// TODO: handle exception
		} finally {
			try {
				if (rs != null)
					rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if (conn != null)
					conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		end = System.currentTimeMillis();
		out.println("========================");
		out.println("==== !! Please wait !!  =====");
		System.out.print("     compare");
		try {
			for (int i = 0; i < 5; i++) {
				Thread.sleep(1000);
				System.out.print(".");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		out.println("\nMYSQL : " + time + " millis");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		out.println("PostgreSQL : " + (end - start) + " millis");
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long comp = time - (end - start);
		String compStr = (comp == 0) ? "=" : ((comp > 0) ? "<" : ">");
		out.println("Speed comparison result result: MYSQL "
				+ compStr + " PostgreSQL");
		out.println("siranakatta");
	%>
</body>
</html>